package com.jeeplus.modules.wms.util;

import java.lang.reflect.Field;
         import java.lang.reflect.Method;

         public class CopyUtil{

              /**
   8      * 复制sour里属性不为空的值到obje为空的属性
   9      *
   10      * @param obje
   11      *            目标实体类
   12      * @param sour
   13      *            源实体类
   14      * @param isCover
   15      *            是否保留obje类里不为null的属性值(保留源值，属性为null则赋值)
   16      * @return obje
   17      */
              public static Object Copy(Object obje, Object sour, boolean isCover) {
               Field[] fields = sour.getClass().getDeclaredFields();
              for (int i = 0, j = fields.length; i < j; i++) {
                        String propertyName = fields[i].getName();
                        Object propertyValue = getProperty(sour, propertyName);
                      if (isCover) {
                                 if (getProperty(obje, propertyName) == null
                                        && propertyValue != null) {
                                        Object setProperty = setProperty(obje, propertyName,
                                                        propertyValue);
                                    }
                         } else {
                                Object setProperty = setProperty(obje, propertyName,
                                                 propertyValue);
                           }

                   }
                return obje;
             }

              /**
  39      * 得到值
  40      *
  41      * @param bean
  42      * @param propertyName
  43      * @return
  44      */
           private static Object getProperty(Object bean, String propertyName) {
                Class clazz = bean.getClass();
               try {
                        Field field = clazz.getDeclaredField(propertyName);
                         Method method = clazz.getDeclaredMethod(
                                         getGetterName(field.getName()), new Class[] {});
                     return method.invoke(bean, new Object[] {});
                   } catch (Exception e) {
                     }
              return null;
            }

            /**
  58      * 给bean赋值
  59      *
  60      * @param bean
  61      * @param propertyName
  62      * @param value
  63      * @return
  64      */
             private static Object setProperty(Object bean, String propertyName,
              Object value) {
                 Class clazz = bean.getClass();
                try {
                        Field field = clazz.getDeclaredField(propertyName);
                         Method method = clazz.getDeclaredMethod(
                                        getSetterName(field.getName()),
                                         new Class[] { field.getType() });
                         return method.invoke(bean, new Object[] { value });
                  } catch (Exception e) {
                     }
                 return null;
           }

              /**
  80      * 根据变量名得到get方法
  81      *
  82      * @param propertyName
  83      * @return
  84      */
              private static String getGetterName(String propertyName) {
                String method = "get" + propertyName.substring(0, 1).toUpperCase()
                         + propertyName.substring(1);
                 return method;
             }

              /**
  92      * 得到setter方法
  93      *
  94      * @param propertyName
  95      *            变量名
  96      * @return
  97      */
             private static String getSetterName(String propertyName) {
                 String method = "set" + propertyName.substring(0, 1).toUpperCase()
                         + propertyName.substring(1);
                return method;
         }

}
